﻿<!--#include file="/parts/head.html" -->
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
  <h1 id="title" data-navbaractive="navdashboard" class="h2">Dashboard</h1>
</div>

<h3>Running Miners <a href="#" id="pauseminers" class="btn btn-primary">Pause / Restart</a></h3>
<table id="miners" class="table mb-4"
  data-toggle="table"
  data-url="/runningminers"
  data-response-handler="formatRunningMiners"
  data-sort-order="desc"
  data-sort-name="Profit"
  data-cache="false"
  data-show-columns="true"
  data-show-toggle="true"
  data-show-refresh="true"
  data-icons-prefix="fa"
  data-icons="icons"
  data-detail-view="true"
  data-detail-formatter="detailFormatter"
>
  <thead>
      <tr>
          <th data-field="Name" data-sortable="true" data-filter-control="select" data-filter-strict-search="true">Name</th>
          <th data-field="Pool" data-sortable="true" data-filter-control="select" data-filter-strict-search="true">Pool</th>
          <th data-field="tDevices" data-sortable="true" data-filter-control="select" data-filter-strict-search="true">Devices</th>
          <th data-field="Profit" data-align="right" data-sortable="true" data-formatter="formatBTC">Profit</th>
          <th data-field="tPowerDraw" data-sortable="true">Power</th>
          <th data-field="tPrimaryAlgorithm" data-sortable="true" data-filter-control="select" data-filter-strict-search="true">Algo</th>
          <th data-field="tPrimaryHashRateLive" data-sortable="true" data-formatter="formatHashRateValue">Speed</th>
          <th data-field="tPrimaryHashRate" data-sortable="true" data-formatter="formatHashRateValue" data-filter-stric-search="true">Benched</th>
          <th data-field="tSecondaryAlgorithm" data-sortable="true" data-filter-control="select" data-filter-strict-search="true">2nd Algo</th>
          <th data-field="tSecondaryHashRateLive" data-sortable="true" data-formatter="formatHashRateValue">2nd Speed</th>
          <th data-field="tSecondaryHashRate" data-sortable="true" data-formatter="formatHashRateValue">2nd Benched</th>
      </tr>
  </thead>
</table>

<h3 class="mt-4">Balances</h3>
<div id="balanceboxes" class="card-deck"></div>

<!-- End of page scripts -->
<script id="poolbalance_template" type="text/x-handlebars-template">
  <div class="mb-2 card {{#if total}}text-white bg-primary {{else}}text-dark bg-light{{/if}}" style="min-width: {{#if total}}200px{{else}}200px{{/if}}";>
    <div class="card-header text-center">
      <h5>{{name}}</h5>
    </div>
    <div class="card-body text-center">
      <p class="card-text currencies">
        {{#each balances}}
          <span class="currencyname font-weight-bold">{{@key}}:</span> <span>{{this}}</span><br/>
        {{/each}}
      </p>
    </div>
  </div>
</script>

<script type="text/javascript">
    $(function () {
        var template = Handlebars.compile($("#poolbalance_template").html());
        $("#pauseminers").click(function () {
            $.ajax({
                url: '/pause', success: function (result) {
                    $("#pauseminers").html(result ? "Restart" : "Pause");
                }
            })
        });
        var nDecimalSeparator = ".", tDecimalSeparator = ",";
        $.ajax({
            url: '/decsep', success: function (result) {
                nDecimalSeparator = result
                tDecimalSeparator = nDecimalSeparator == "." ? "," : "."
            }
        });

        (function updatePoolBalances() {
            $.ajax({
                url: '/balances', success: function (result) {
                    //console.log(result);
                    $("#balanceboxes").empty();
                    var boxes = [], boxes_ix = {};
                    $.each(result, function (index, item) {
                        var properties = Object.keys(item);
                        var currencies = properties.filter((properties) => properties.startsWith("Value in "));

                        if (boxes_ix[item.Name] === undefined) {
                            boxes_ix[item.Name] = boxes.length;
                            if (item.Name == "*Total*") {
                                boxes.push({ name: "Total Balance From All Pools", balances: {}, digits: {}, total: true });
                            } else {
                                boxes.push({ name: item.Name, balances: {}, digits: {} });
                            }
                        }
                        $.each(currencies, function (cindex, currency) {
                            var name = currency.replace("Value in ", "");
                            item[currency] = item[currency].replace(tDecimalSeparator, "").replace(nDecimalSeparator, ".")
                            var csplit = item[currency].split(".");
                            var digits = boxes[boxes_ix[item.Name]]["digits"][name] = item[currency] === "-" ? -1 : (csplit.length < 2 ? 0 : csplit[1].length);
                            if (digits != -1) { item[currency] = parseFloat(item[currency]) }
                            if (boxes[boxes_ix[item.Name]]["balances"][name] === undefined) { boxes[boxes_ix[item.Name]]["balances"][name] = item[currency]; }
                            else if (digits != -1) {
                                if (boxes[boxes_ix[item.Name]]["balances"][name] === "-") boxes[boxes_ix[item.Name]]["balances"][name] = 0.0;
                                boxes[boxes_ix[item.Name]]["balances"][name] += parseFloat(item[currency]);
                            }
                        });
                    });
                    $.each(boxes, function (index, item) {
                        $.each(item["balances"], function (bindex, bitem) {if (item["digits"][bindex] != -1) item["balances"][bindex] = bitem.toFixed(item["digits"][bindex]).replace(".", ",");});
                        $("#balanceboxes").append(template(item));
                    });
                    window.setTimeout(updatePoolBalances, 60000);
                }
            });
        })();
        (function updateStatus() {
            $.ajax({
                url: '/status', success: function (result) {
                    $("#pauseminers").html(result.Pause? "Restart" : "Pause");
                    window.setTimeout(updateStatus, 10000);
                }
            });
        })();
        function refreshTable() {
            $("table.table").bootstrapTable("refresh", { silent: true });
            window.setTimeout(refreshTable, 10000);
        }
        window.setTimeout(refreshTable, 10000);
    });
  
  function formatRunningMiners(data) {
      // This function can alter the returned data before building the table, formatting it in a way
      // that is easier to display and manipulate in a table
      $.each(data, function (index, item) {
          // Format the type(s)
          //console.log(data);
          //console.log(item);
          item.tDevices = item.DeviceModel.toString();
          item.Name = item.Name.split(/\-/)[0];

          // Format the algorithms and hashrates

          // Algorithm is always an array, sometimes has 2 elements
          item.tPrimaryAlgorithm = item.Algorithm[0];
          try { item.tSecondaryAlgorithm = item.Algorithm[1]; } catch (error) { /* ignore */ }

          // Speed is an array if there are multiple algorithms, or a single number otherwise
          if (Array.isArray(item.Speed)) {
              item.tPrimaryHashRate = item.Speed[0];
              item.tSecondaryHashRate = item.Speed[1];
          } else {
              item.tPrimaryHashRate = item.Speed;
          }

          if (Array.isArray(item.Speed_Live)) {
              item.tPrimaryHashRateLive = item.Speed_Live[0];
              item.tSecondaryHashRateLive = item.Speed_Live[1];
          } else {
              item.tPrimaryHashRateLive = item.Speed_Live;
          }
          item.tPowerDraw = Math.round(parseFloat(item.PowerDraw)) + ' W';
      });
      return data;
  }
</script>
<!--#include file="/parts/foot.html" -->